import React, { Component } from 'react'
import { Form, Button, Switch, Radio, Checkbox } from 'antd'
import { messages } from "share/common";
import Condition from 'components/condition'
import Priority from "components/template/priority";
import constant from "../constant";
import SaveHoc from "./save-hoc";

class RefundRule extends Component {

  state = {
    priority: null,
    saveLoading: false
  };

  componentWillMount() {
    const { record } = this.props.params;
    this.setState({
      priority: record.optionId ? {
        levelOrgName: record.levelOrgName,
        levelOrgId: record.levelOrgId,
        distribution: record.isGlobal ? null : {
          companies: record.fmRepaymentOptionDistList.map(item => { return { name: item.levelOrgName, id: item.levelOrgId } })
        }
      } : null
    })
  };

  options = [
    { label: messages('finance-1.key244')/*启用temp*/, value: 'true' },
    { label: messages('finance-1.key266')/*不启用*/, value: 'false' }
  ];

  handleSave = () => {
    this.props.form.validateFieldsAndScroll((err, value) => {
      if (!err) {
        this.props.handleSave('saveRefundRule', value, 'fmRepaymentOptionDistList');
      }
    });
  };

  handlePriority = value => {
    this.setState({ priority: value });
  };

  render() {
    const { getFieldDecorator, getFieldValue } = this.props.form;
    const { saveLoading, priority } = this.state;
    const { close } = this.props, { record } = this.props.params;
    const { formItemLayout } = constant;
    return (
      <div>
        <Form className={'new-form'}>
          <Condition>
            <Condition.If value={record.onlyRead}>
              <Form.Item
                label={messages('finance-1.key265')/*启用核销*/}
                {...formItemLayout}
                extra={messages('finance-1.key438')/*允许报销单核销借款单，启用后报销单付款行显示核销入口*/}
                required
              >
                <div>{messages('finance-1.key244')/*启用temp*/}</div>
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key267')/*多核销*/}
                {...formItemLayout}
                extra={messages('finance-1.key439')/*允许1个报销单付款行，核销多笔借款付款流水*/}
                required
              >
                <div>{messages('finance-1.key244')/*启用temp*/}</div>
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key268')/*专项核销*/}
                {...formItemLayout}
                extra={messages('finance-1.key440')/*启用后，仅能核销同一申请单或合同关联的欠款，若无此类欠款，则可以核销所有*/}
                required
              >
                <div>{messages('finance-1.key266')/*不启用*/}</div>
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key269')/*核销币种规则*/}
                {...formItemLayout}
                extra={messages('finance-1.key441')/*付款行币种与借款行币种均为同种外币的核销*/}
                required
              >
                <div>
                  <div>{messages('finance-1.key442')/*本位币-本位币*/}</div>
                  <div className={'ant-form-extra'}>{messages('finance-1.key443')/*付款行币种与借款行币种均为本位币的核销*/}</div>
                  <div style={{ marginTop: '16px' }}>{messages('finance-1.key444')/*外币-外币（同币种）*/}</div>
                </div>
              </Form.Item>
              <h3 className='sub-title'>{messages('finance-1.key445')/*还款配置*/}</h3>
              <Form.Item
                label={messages('finance-1.key271')/*启用还款单*/}
                {...formItemLayout}
                extra={messages('finance-1.key446')/*允许创建还款单进行还款*/}
                required
              >
                <div>{messages('finance-1.key244')/*启用temp*/}</div>
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key7')/*还款方式*/}
                {...formItemLayout}
                extra={messages('finance-1.key447')/*使用额度进行还款，无现金流产生，仅提供给财务创建还款使用*/}
                required
              >
                <div>
                  <div>{messages('finance-1.key8')/*现金还款*/}</div>
                  <div className={'ant-form-extra'}>{messages('finance-1.key448')/*使用现金进行还款*/}</div>
                  <div style={{ marginTop: '16px' }}>{messages('finance-1.key9')/*转账还款*/}</div>
                  <div className={'ant-form-extra'}>{messages('finance-1.key449')/*使用银行转账进行还款*/}</div>
                  <div style={{ marginTop: '16px' }}>{messages('finance-1.key204')/*额度还款*/}</div>
                </div>
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key272')/*部分还款*/}
                {...formItemLayout}
                extra={messages('finance-1.key450')/*员工创建还款单时，可修改还款金额*/}
                required
              >
                <div>{messages('finance-1.key244')/*启用temp*/}</div>
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key273')/*财务确认可修改*/}
                {...formItemLayout}
                extra={messages('finance-1.key451')/*财务确认还款单时，可修改员工提交的还款金额*/}
                required
              >
                <div>{messages('finance-1.key244')/*启用temp*/}</div>
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key274')/*附件必传*/}
                {...formItemLayout}
                extra={messages('finance-1.key452')/*转账还款时，附件图片必须上传*/}
                required
              >
                <div>{messages('finance-1.key244')/*启用temp*/}</div>
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key453')/*财务还款撤回*/}
                {...formItemLayout}
                extra={messages('finance-1.key454')/*财务直接创建的还款单，提交后允许撤回并删除*/}
                required
              >
                <div>{messages('finance-1.key266')/*不启用*/}</div>
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key455')/*还款方账户必填*/}
                {...formItemLayout}
                extra={messages('finance-1.key456')/*创建还款单时，还款付款账户信息是否设置为必填*/}
                required
              >
                <div>{messages('finance-1.key244')/*启用temp*/}</div>
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key457')/*还款转交*/}
                {...formItemLayout}
                extra={messages('finance-1.key458')/*已付清未还清的员工借款、未还清的供应商预付款可以转交给其他员工，被转交人可以操作还款、报销核销*/}
                required
              >
                <div>{messages('finance-1.key266')/*不启用*/}</div>
              </Form.Item>
            </Condition.If>
            <Condition.Else>
              <Form.Item
                label={messages('common.column.status')/*状态*/}
                {...formItemLayout}
              >
                {getFieldDecorator("isEnable", {
                  rules: [{ required: true }],
                  valuePropName: "checked",
                  initialValue: record.hasOwnProperty('isEnable') ? record.isEnable : 'true'
                })(
                  <Switch>{messages('finance-1.key244')/*启用temp*/}</Switch>
                )}
              </Form.Item>
              <Priority
                disabledTop={record.hasOwnProperty('optionId')}
                top='SET_OF_BOOK'
                onChange={this.handlePriority}
                formItemLayout={formItemLayout}
                defaultValue={priority}
                single={false}
                showCompany={false}
              />
              <h3 className='sub-title'>{messages('finance-1.key459')/*核销配置*/}</h3>
              <Form.Item
                label={messages('finance-1.key265')/*启用核销*/}
                {...formItemLayout}
                extra={messages('finance-1.key438')/*允许报销单核销借款单，启用后报销单付款行显示核销入口*/}
              >
                {getFieldDecorator("option1", {
                  rules: [{ required: true }],
                  initialValue: record.hasOwnProperty('option1') ? record.option1 : 'true'
                })(
                  <Radio.Group options={this.options}/>
                )}
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key524')/*跨公司核销*/}
                {...formItemLayout}
                extra={messages('finance-1.key525')/*启用后，支持同账套下收款人跨公司或法人进行核销*/}
              >
                {getFieldDecorator("option13", {
                  rules: [{ required: true }],
                  initialValue: record.hasOwnProperty('option13') ? record.option13 : 'false'
                })(
                  <Radio.Group options={this.options}/>
                )}
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key267')/*多核销*/}
                {...formItemLayout}
                extra={messages('finance-1.key439')/*允许1个报销单付款行，核销多笔借款付款流水*/}
              >
                {getFieldDecorator("option2", {
                  rules: [{ required: true }],
                  initialValue: record.hasOwnProperty('option2') ? record.option2 : 'true'
                })(
                  <Radio.Group options={this.options}/>
                )}
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key268')/*专项核销*/}
                {...formItemLayout}
                extra={messages('finance-1.key440')/*启用后，仅能核销同一申请单或合同关联的欠款，若无此类欠款，则可以核销所有*/}
              >
                {getFieldDecorator("option3", {
                  rules: [{ required: true }],
                  initialValue: record.hasOwnProperty('option3') ? record.option3 : 'false'
                })(
                  <Radio.Group options={this.options}/>
                )}
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key269')/*核销币种规则*/}
                {...formItemLayout}
              >
                {getFieldDecorator("writeOffRules", {
                  rules: [{ required: true, message: messages('common.select')/*请选择*/ }],
                  initialValue: record.hasOwnProperty('writeOffRules') ? record.writeOffRules.map(item => item + '') : ['1', '8']
                })(
                  <Checkbox.Group>
                    <Checkbox value='1'>{messages('finance-1.key442')/*本位币-本位币*/}</Checkbox>
                    <div className='ant-form-extra'>{messages('finance-1.key443')/*付款行币种与借款行币种均为本位币的核销*/}</div>
                    <Checkbox value='8' style={{ marginTop: '16px' }}>{messages('finance-1.key460')/*本位币-外币*/}</Checkbox>
                    <div className='ant-form-extra'>{messages('finance-1.key461')/*付款行币种为本位币，借款行币种为外币的核销*/}</div>
                    <Checkbox value='4' style={{ marginTop: '16px' }}>{messages('finance-1.key462')/*外币-本外币*/}</Checkbox>
                    <div className='ant-form-extra'>{messages('finance-1.key463')/*付款行币种为外币，借款行币种为本位币的核销*/}</div>
                    <Checkbox value='2' style={{ marginTop: '16px' }}>{messages('finance-1.key444')/*外币-外币（同币种）*/}</Checkbox>
                    <div className='ant-form-extra'>{messages('finance-1.key441')/*付款行币种与借款行币种均为同种外币的核销*/}</div>
                  </Checkbox.Group>
                )}
              </Form.Item>
              <h3 className='sub-title'>{messages('finance-1.key464')/*还款控制*/}</h3>
              <Form.Item
                label={messages('finance-1.key271')/*启用还款单*/}
                {...formItemLayout}
                extra={messages('finance-1.key446')/*允许创建还款单进行还款*/}
              >
                {getFieldDecorator("option5", {
                  rules: [{ required: true }],
                  initialValue: record.hasOwnProperty('option5') ? record.option5 : 'true'
                })(
                  <Radio.Group options={this.options}/>
                )}
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key7')/*还款方式*/}
                {...formItemLayout}
              >
                {getFieldDecorator("repaymenTypes", {
                  rules: [{ required: true, message: messages('common.select')/*请选择*/ }],
                  initialValue: record.hasOwnProperty('repaymenTypes') ? record.repaymenTypes.map(item => item + '') : ['1', '2', '4']
                })(
                  <Checkbox.Group>
                    <Checkbox value='1'>{messages('finance-1.key8')/*现金还款*/}</Checkbox>
                    <div className='ant-form-extra'>{messages('finance-1.key448')/*使用现金进行还款*/}</div>
                    <Checkbox value='2' style={{ marginTop: '16px' }}>{messages('finance-1.key9')/*转账还款*/}</Checkbox>
                    <div className='ant-form-extra'>{messages('finance-1.key449')/*使用银行转账进行还款*/}</div>
                    <Checkbox value='4' style={{ marginTop: '16px' }}>{messages('finance-1.key204')/*额度还款*/}</Checkbox>
                    <div className='ant-form-extra'>{messages('finance-1.key447')/*使用额度进行还款，无现金流产生，仅提供给财务创建还款使用*/}</div>
                  </Checkbox.Group>
                )}
              </Form.Item>
              {
                getFieldValue('repaymenTypes').indexOf('2') > -1 && (
                  <Form.Item
                    label={messages('finance-1.key542')/*跨公司还款*/}
                    {...formItemLayout}
                    extra={messages('finance-1.key543')/*若不启用，收款方信息中“开户公司”不允许修改*/}
                  >
                    {getFieldDecorator("option14", {
                      rules: [{ required: true }],
                      initialValue: record.hasOwnProperty('option14') ? record.option14 : 'true'
                    })(
                      <Radio.Group options={this.options}/>
                    )}
                  </Form.Item>
                )
              }
              <Form.Item
                label={messages('finance-1.key272')/*部分还款*/}
                {...formItemLayout}
                extra={messages('finance-1.key450')/*员工创建还款单时，可修改还款金额*/}
              >
                {getFieldDecorator("option7", {
                  rules: [{ required: true }],
                  initialValue: record.hasOwnProperty('option7') ? record.option7 : 'true'
                })(
                  <Radio.Group options={this.options}/>
                )}
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key273')/*财务确认可修改*/}
                {...formItemLayout}
                extra={messages('finance-1.key451')/*财务确认还款单时，可修改员工提交的还款金额*/}
              >
                {getFieldDecorator("option8", {
                  rules: [{ required: true }],
                  initialValue: record.hasOwnProperty('option8') ? record.option8 : 'true'
                })(
                  <Radio.Group options={this.options}/>
                )}
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key274')/*附件必传*/}
                {...formItemLayout}
                extra={messages('finance-1.key452')/*转账还款时，附件图片必须上传*/}
              >
                {getFieldDecorator("option9", {
                  rules: [{ required: true }],
                  initialValue: record.hasOwnProperty('option9') ? record.option9 : 'true'
                })(
                  <Radio.Group options={this.options}/>
                )}
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key453')/*财务还款撤回*/}
                {...formItemLayout}
                extra={messages('finance-1.key454')/*财务直接创建的还款单，提交后允许撤回并删除*/}
              >
                {getFieldDecorator("option10", {
                  rules: [{ required: true }],
                  initialValue: record.hasOwnProperty('option10') ? record.option10 : 'false'
                })(
                  <Radio.Group options={this.options}/>
                )}
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key455')/*还款方账户必填*/}
                {...formItemLayout}
                extra={messages('finance-1.key456')/*创建还款单时，还款付款账户信息是否设置为必填*/}
              >
                {getFieldDecorator("option11", {
                  rules: [{ required: true }],
                  initialValue: record.hasOwnProperty('option11') ? record.option11 : 'true'
                })(
                  <Radio.Group options={this.options}/>
                )}
              </Form.Item>
              <Form.Item
                label={messages('finance-1.key457')/*还款转交*/}
                {...formItemLayout}
                extra={messages('finance-1.key458')/*已付清未还清的员工借款、未还清的供应商预付款可以转交给其他员工，被转交人可以操作还款、报销核销*/}
              >
                {getFieldDecorator("option12", {
                  rules: [{ required: true }],
                  initialValue: record.hasOwnProperty('option12') ? record.option12 : 'false'
                })(
                  <Radio.Group options={this.options}/>
                )}
              </Form.Item>
            </Condition.Else>
          </Condition>
        </Form>
        <div className='footer-operate'>
          <Condition.If value={!record.onlyRead}>
            <Button type='primary' onClick={this.handleSave} loading={saveLoading}>{messages('common.ok')/*确定*/}</Button>
          </Condition.If>
          <Button onClick={() => close()}>{record.onlyRead ? messages('common.back')/*返回*/ : messages('common.cancel')/*取消*/}</Button>
        </div>
      </div>
    )
  }

}

export default Form.create()(SaveHoc(RefundRule));
